
## For Figure S12, please see 'main_text_figs_JEPP_R1'

######### robustness check for argument level confounders

#load("dataset.RData")
#head(df) 

vars<-c("ID", "framing_parity_pro1"  ,         "framing_parity_pro2"    ,       "framing_parity_anti1"    ,     
 "framing_parity_anti2"      ,    "framing_quota_pro1"     ,       "framing_quota_pro2"       ,    
"framing_quota_anti1"        ,   "framing_quota_anti2")
X2<-df[,vars]

head(X2)
nrow(X2)


#load("all_merged.RData")

uk <- subset(all, country=="UK")

test1<- merge(uk, X2, by=c("ID"), all.x=TRUE) 
head(test1)
nrow(test1) 

uk2 <- test1[test1$Treatment != "Control", ]
uk2$Treatment <- droplevels(uk2$Treatment)

uk2$resp_gender <- droplevels(uk2$resp_gender)

sapply(uk2, class)

library(dplyr)

# Merge framing_parity_pro1 and framing_quota_pro1
uk2$framing_pro1_combined <- factor(coalesce(uk2$framing_parity_pro1, uk2$framing_quota_pro1))

# Merge framing_parity_pro2 and framing_quota_pro2
uk2$framing_pro2_combined <- factor(coalesce(uk2$framing_parity_pro2, uk2$framing_quota_pro2))

# Merge framing_parity_anti1 and framing_quota_anti1
uk2$framing_anti1_combined <- factor(coalesce(uk2$framing_parity_anti1, uk2$framing_quota_anti1))

# Merge framing_parity_anti2 and framing_quota_anti2
uk2$framing_anti2_combined <- factor(coalesce(uk2$framing_parity_anti2, uk2$framing_quota_anti2))

### create dummy variables for pro argument seen 
table(uk2$framing_pro1_combined )
table(uk2$framing_pro2_combined )

uk2$Necessary<-0
uk2$Necessary[uk2$framing_pro1_combined=="Necessary"]<-1
uk2$Necessary[uk2$framing_pro2_combined=="Necessary"]<-1
table(uk2$Necessary)

uk2$Style<-0
uk2$Style[uk2$framing_pro1_combined=="Style"]<-1
uk2$Style[uk2$framing_pro2_combined=="Style"]<-1
table(uk2$Style)

uk2$Policy<-0
uk2$Policy[uk2$framing_pro1_combined=="Policy"]<-1
uk2$Policy[uk2$framing_pro2_combined=="Policy"]<-1
table(uk2$Policy)

uk2$Democracy<-0
uk2$Democracy[uk2$framing_pro1_combined=="Democracy"]<-1
uk2$Democracy[uk2$framing_pro2_combined=="Democracy"]<-1
table(uk2$Democracy)

uk2$Half<-0
uk2$Half[uk2$framing_pro1_combined=="Half of Population"]<-1
uk2$Half[uk2$framing_pro2_combined=="Half of Population"]<-1
table(uk2$Half)

uk2$PolBenefits<-0
uk2$PolBenefits[uk2$framing_pro1_combined=="Political Benefits"]<-1
uk2$PolBenefits[uk2$framing_pro2_combined=="Political Benefits"]<-1
table(uk2$PolBenefits)

uk2$Pace<-0
uk2$Pace[uk2$framing_pro1_combined=="Pace"]<-1
uk2$Pace[uk2$framing_pro2_combined=="Pace"]<-1
table(uk2$Pace)

uk2$Other<-0
uk2$Other[uk2$framing_pro1_combined=="Other Countries"]<-1
uk2$Other[uk2$framing_pro2_combined=="Other Countries"]<-1
table(uk2$Other)

uk2$OtherWomen<-0
uk2$OtherWomen[uk2$framing_pro1_combined=="Other Women"]<-1
uk2$OtherWomen[uk2$framing_pro2_combined=="Other Women"]<-1
table(uk2$OtherWomen)

### create dummy variables for anti argument seen
table(uk2$framing_anti1_combined )
table(uk2$framing_anti2_combined )

uk2$Unnecessary<-0
uk2$Unnecessary[uk2$framing_anti1_combined=="Unnecessary"]<-1
uk2$Unnecessary[uk2$framing_anti2_combined=="Unnecessary"]<-1
table(uk2$Unnecessary)

uk2$LowerImpact<-0
uk2$LowerImpact[uk2$framing_anti1_combined=="Lower Impact"]<-1
uk2$LowerImpact[uk2$framing_anti2_combined=="Lower Impact"]<-1
table(uk2$LowerImpact)

uk2$Suspicion<-0
uk2$Suspicion[uk2$framing_anti1_combined=="Suspicion"]<-1
uk2$Suspicion[uk2$framing_anti2_combined=="Suspicion"]<-1
table(uk2$Suspicion)

uk2$Unqualified<-0
uk2$Unqualified[uk2$framing_anti1_combined=="Unqualified Candidates"]<-1
uk2$Unqualified[uk2$framing_anti2_combined=="Unqualified Candidates"]<-1
table(uk2$Unqualified)

uk2$Volvo<-0
uk2$Volvo[uk2$framing_anti1_combined=="Volvo Owners Next"]<-1
uk2$Volvo[uk2$framing_anti2_combined=="Volvo Owners Next"]<-1
table(uk2$Volvo)

uk2$Uncertain<-0
uk2$Uncertain[uk2$framing_anti1_combined=="Uncertain Benefits"]<-1
uk2$Uncertain[uk2$framing_anti2_combined=="Uncertain Benefits"]<-1
table(uk2$Uncertain)

uk2$History<-0
uk2$History[uk2$framing_anti1_combined=="History Shows"]<-1
uk2$History[uk2$framing_anti2_combined=="History Shows"]<-1
table(uk2$History)

uk2$NotLaw<-0
uk2$NotLaw[uk2$framing_anti1_combined=="Not Issue of Law"]<-1
uk2$NotLaw[uk2$framing_anti2_combined=="Not Issue of Law"]<-1
table(uk2$NotLaw)

uk2$Symptoms<-0
uk2$Symptoms[uk2$framing_anti1_combined=="Only Treat Symptoms"]<-1
uk2$Symptoms[uk2$framing_anti2_combined=="Only Treat Symptoms"]<-1
table(uk2$Symptoms)

head(uk2)
### all the arguments to include 
# Necessary + Style + Policy + Democracy + Half + PolBenefits + Pace + Other +
# OtherWomen + Unnecessary + LowerImpact + Suspicion + Unqualified + Volvo + 
# Uncertain + History + NotLaw + Symptoms



### First test if any arguments are related to Treatment (ie appear more in one than the other)

check <- lm(as.numeric(Treatment) ~ Necessary + Style + Policy + Democracy + Half + PolBenefits + Pace + Other +
OtherWomen + Unnecessary + LowerImpact + Suspicion + Unqualified + Volvo + 
Uncertain + History + NotLaw + Symptoms, data = uk2, weights = Weight)
summary(check) 

## as expected, none are related to the Treatment 

### Now test whether any arguments are related to support

res1o <- lm(Support ~  Necessary + Style + Policy + Democracy + Half + PolBenefits + Pace + Other +
 OtherWomen + Unnecessary + LowerImpact + Suspicion + Unqualified + Volvo + 
 Uncertain + History + NotLaw + Symptoms , data = uk2, weights = Weight)
summary(res1o) 

### none of them are hugely impactful. Unnecessary is the exception (anti argument) -- increases support at .1 level 

res1 <- lm(Support ~ Treatment + Unnecessary , data = uk2, weights = Weight)
summary(res1) ## 2 get dropped, that is fine 

res2 <- lm(Support ~ Treatment*resp_gender +  Unnecessary , data = uk2, weights = Weight)
summary(res2)

res3 <- lm(Support ~ Treatment*resp_gender*Masculine +  Unnecessary , data = uk2, weights = Weight)
summary(res3)

### our main findings hold 

# France next
#load("FRdataset.RData")
#head(df) 

vars<-c("ID", "framing_parity_pro1"  ,         "framing_parity_pro2"    ,       "framing_parity_anti1"    ,     
 "framing_parity_anti2"      ,    "framing_quota_pro1"     ,       "framing_quota_pro2"       ,    
"framing_quota_anti1"        ,   "framing_quota_anti2")
X2<-df[,vars]
head(X2)
nrow(X2)

fr <- subset(all, country=="FR")

test2<- merge(fr, X2, by=c("ID"), all.x=TRUE) 
head(test2)
nrow(test2) 
nrow(fr)

fr2 <- test2[test2$Treatment != "Control", ]
fr2$Treatment <- droplevels(fr2$Treatment)

fr2$resp_gender <- droplevels(fr2$resp_gender )


# Merge framing_parity_pro1 and framing_quota_pro1
fr2$framing_pro1_combined <- factor(coalesce(fr2$framing_parity_pro1, fr2$framing_quota_pro1))

# Merge framing_parity_pro2 and framing_quota_pro2
fr2$framing_pro2_combined <- factor(coalesce(fr2$framing_parity_pro2, fr2$framing_quota_pro2))

# Merge framing_parity_anti1 and framing_quota_anti1
fr2$framing_anti1_combined <- factor(coalesce(fr2$framing_parity_anti1, fr2$framing_quota_anti1))

# Merge framing_parity_anti2 and framing_quota_anti2
fr2$framing_anti2_combined <- factor(coalesce(fr2$framing_parity_anti2, fr2$framing_quota_anti2))

### create dummy variables for pro argument seen 
table(fr2$framing_pro1_combined )
table(fr2$framing_pro2_combined )

fr2$Necessary<-0
fr2$Necessary[fr2$framing_pro1_combined=="Necessary"]<-1
fr2$Necessary[fr2$framing_pro2_combined=="Necessary"]<-1
table(fr2$Necessary)

fr2$Style<-0
fr2$Style[fr2$framing_pro1_combined=="Style"]<-1
fr2$Style[fr2$framing_pro2_combined=="Style"]<-1
table(fr2$Style)

fr2$Policy<-0
fr2$Policy[fr2$framing_pro1_combined=="Policy"]<-1
fr2$Policy[fr2$framing_pro2_combined=="Policy"]<-1
table(fr2$Policy)

fr2$Democracy<-0
fr2$Democracy[fr2$framing_pro1_combined=="Democracy"]<-1
fr2$Democracy[fr2$framing_pro2_combined=="Democracy"]<-1
table(fr2$Democracy)

fr2$Half<-0
fr2$Half[fr2$framing_pro1_combined=="Half of Population"]<-1
fr2$Half[fr2$framing_pro2_combined=="Half of Population"]<-1
table(fr2$Half)

fr2$PolBenefits<-0
fr2$PolBenefits[fr2$framing_pro1_combined=="Political Benefits"]<-1
fr2$PolBenefits[fr2$framing_pro2_combined=="Political Benefits"]<-1
table(fr2$PolBenefits)

fr2$Pace<-0
fr2$Pace[fr2$framing_pro1_combined=="Pace"]<-1
fr2$Pace[fr2$framing_pro2_combined=="Pace"]<-1
table(fr2$Pace)

fr2$Other<-0
fr2$Other[fr2$framing_pro1_combined=="Other Countries"]<-1
fr2$Other[fr2$framing_pro2_combined=="Other Countries"]<-1
table(fr2$Other)

fr2$OtherWomen<-0
fr2$OtherWomen[fr2$framing_pro1_combined=="Other Women"]<-1
fr2$OtherWomen[fr2$framing_pro2_combined=="Other Women"]<-1
table(fr2$OtherWomen)

### create dummy variables for anti argument seen
table(fr2$framing_anti1_combined )
table(fr2$framing_anti2_combined )

fr2$Unnecessary<-0
fr2$Unnecessary[fr2$framing_anti1_combined=="Unnecessary"]<-1
fr2$Unnecessary[fr2$framing_anti2_combined=="Unnecessary"]<-1
table(fr2$Unnecessary)

fr2$LowerImpact<-0
fr2$LowerImpact[fr2$framing_anti1_combined=="Lower Impact"]<-1
fr2$LowerImpact[fr2$framing_anti2_combined=="Lower Impact"]<-1
table(fr2$LowerImpact)

fr2$Suspicion<-0
fr2$Suspicion[fr2$framing_anti1_combined=="Suspicion"]<-1
fr2$Suspicion[fr2$framing_anti2_combined=="Suspicion"]<-1
table(fr2$Suspicion)

fr2$Unqualified<-0
fr2$Unqualified[fr2$framing_anti1_combined=="Unqualified Candidates"]<-1
fr2$Unqualified[fr2$framing_anti2_combined=="Unqualified Candidates"]<-1
table(fr2$Unqualified)

fr2$Volvo<-0
fr2$Volvo[fr2$framing_anti1_combined=="Volvo Owners Next"]<-1
fr2$Volvo[fr2$framing_anti2_combined=="Volvo Owners Next"]<-1
table(fr2$Volvo)

fr2$Uncertain<-0
fr2$Uncertain[fr2$framing_anti1_combined=="Uncertain Benefits"]<-1
fr2$Uncertain[fr2$framing_anti2_combined=="Uncertain Benefits"]<-1
table(fr2$Uncertain)

fr2$History<-0
fr2$History[fr2$framing_anti1_combined=="History Shows"]<-1
fr2$History[fr2$framing_anti2_combined=="History Shows"]<-1
table(fr2$History)

fr2$NotLaw<-0
fr2$NotLaw[fr2$framing_anti1_combined=="Not Issue of Law"]<-1
fr2$NotLaw[fr2$framing_anti2_combined=="Not Issue of Law"]<-1
table(fr2$NotLaw)

fr2$Symptoms<-0
fr2$Symptoms[fr2$framing_anti1_combined=="Only Treat Symptoms"]<-1
fr2$Symptoms[fr2$framing_anti2_combined=="Only Treat Symptoms"]<-1
table(fr2$Symptoms)

head(fr2)
### all the arguments to include 
# Necessary + Style + Policy + Democracy + Half + PolBenefits + Pace + Other +
# OtherWomen + Unnecessary + LowerImpact + Suspicion + Unqualified + Volvo + 
# Uncertain + History + NotLaw + Symptoms


### First test if any arguments are related to Treatment (ie appear more in one than the other)

check2 <- lm(as.numeric(Treatment) ~ Necessary + Style + Policy + Democracy + Half + PolBenefits + Pace + Other +
OtherWomen + Unnecessary + LowerImpact + Suspicion + Unqualified + Volvo + 
Uncertain + History + NotLaw + Symptoms, data = fr2, weights = Weight)
summary(check2) 

## Other and Volvo are significant at .1 level 

### Now test whether any arguments are related to support

res1o2 <- lm(Support ~  Necessary + Style + Policy + Democracy + Half + PolBenefits + Pace + Other +
 OtherWomen + Unnecessary + LowerImpact + Suspicion + Unqualified + Volvo + 
 Uncertain + History + NotLaw + Symptoms , data = fr2, weights = Weight)
summary(res1o2) 

### none of them are hugely impactful. NotLaw is the exception (anti argument) -- increases support at .1 level 

res1fr <- lm(Support ~ Treatment + NotLaw, data = fr2, weights = Weight)
summary(res1fr) ## result holds -- coefficient is nearly identical as is p-value

res2fr <- lm(Support ~ Treatment*resp_gender +  NotLaw + Other + Volvo, data = fr2, weights = Weight)
summary(res2fr) ## results hold 

res3fr <- lm(Support ~ Treatment*resp_gender*Masculine +  NotLaw + Other + Volvo, data = fr2, weights = Weight)
summary(res3fr) ## results hold 

### our main findings hold


stargazer(check, check2, title="Regression Results", align=TRUE, type="latex")

stargazer(res1o, res1o2, title="Regression Results", align=TRUE, type="latex")

stargazer(res1, res2, res3, res1fr, res2fr, res3fr, title="Regression Results", align=TRUE, type="latex")


######### robustness check for conjoint level confounders

# UK first
# load yougov_uk_conjoint.RData

long$resp_gender <- factor(long$resp_gender, levels = c("Female", "Male"))

# First step of Fong and Grimmer

res <- lm(as.numeric(Treatment) ~ feature2 + feature3 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res)
# political experience, issue areas are significant

#which features are significant?
res1 <- lm(Qualified_score ~ feature2 + feature3 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res1)
# marital status, political experience, issue areas are significant

# include marital status, issue areas, and political experience in main models
res13 <- lm(Qualified_score ~ Treatment + feature2 + feature3 + feature4, data = subset(long, feature1=="Female"), weights = Weight)
summary(res13)

res15 <- lm(Qualified_score ~ Treatment*resp_gender + feature2 + feature3 + feature4, data = subset(long, feature1=="Female"), weights = Weight)
summary(res15)

res17 <- lm(Qualified_score ~ Treatment*resp_gender*Masculine + feature2 + feature3 + feature4, data = subset(long, feature1=="Female"), weights = Weight)
summary(res17)

# main results hold

#FR
# load FRCONJdataset.RData

long$resp_gender <- factor(long$resp_gender, levels = c("Female", "Male"))

# first part of fong and grimmer
res2 <- lm(as.numeric(Treatment) ~ feature2 + feature3 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res2)
# talent is significant 

#which features are significant?
res19 <- lm(Qualified_score ~ feature2 + feature3 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res19)
# talent, career experience, political experience, marital status, age are significant

res21 <- lm(Qualified_score ~ Treatment + feature2 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res21)

res23 <- lm(Qualified_score ~ Treatment*resp_gender + feature2 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res23)


res25 <- lm(Qualified_score ~ Treatment*resp_gender*Masculine + feature2 + feature4 + feature5 + feature6 + feature7, data = subset(long, feature1=="Female"), weights = Weight)
summary(res25)


stargazer(res, res2, title="Regression Results", align=TRUE, type="latex")

stargazer(res1, res19, title="Regression Results", align=TRUE, type="latex")

stargazer(res13, res15, res17, res21, res23, res25, title="Regression Results", align=TRUE, type="latex")



# independence

all <- subset(all, country != "US")

balance_table <- table(all$Treatment)

all$resp_gender <- droplevels(all$resp_gender )

b2 <- table(all$Treatment, all$resp_gender)

b3 <- tapply(all$Masculine, all$Treatment, mean)


balance_table
b2
b3


# UK

variable_names <- c("framing_parity_pro1"  ,         "framing_parity_pro2"     ,    "framing_quota_pro1"     ,       "framing_quota_pro2")

list_of_tables <- lapply(variable_names, function(var) table(df[[var]]))

# Define all possible levels across the 16 variables
levels_vector <- c("Necessary", "Style", "Policy", "Democracy", "Half of Population",
                   "Political Benefits", "Pace", "Other Countries", "Other Women")

# Bind all tables together by row names, ensuring all levels are considered
combined_table <- do.call(cbind, lapply(list_of_tables, function(x) {
  # Initialize a vector with all counts set to 0 for each level
  count <- setNames(rep(0, length(levels_vector)), levels_vector)
  # Update the counts with actual values from the table
  actual_levels <- names(x)
  count[actual_levels] <- x
  # Return the modified count vector as a table object
  as.table(count)
}))

# Transpose the result if needed to align variables as rows instead of columns
combined_table <- t(combined_table)


variable_names <- c("framing_parity_anti1",  "framing_parity_anti2"  ,  "framing_quota_anti1" ,  "framing_quota_anti2")

list_of_tables <- lapply(variable_names, function(var) table(df[[var]]))

# Define all possible levels across the 16 variables
levels_vector <- c("Unnecessary", "Lower Impact", "Suspicion", "Unqualified Candidates",
                   "Volvo Owners Next", "Uncertain Benefits", "History Shows", 
                   "Not Issue of Law", "Only Treat Symptoms")

# Bind all tables together by row names, ensuring all levels are considered
combined_table2 <- do.call(cbind, lapply(list_of_tables, function(x) {
  # Initialize a vector with all counts set to 0 for each level
  count <- setNames(rep(0, length(levels_vector)), levels_vector)
  # Update the counts with actual values from the table
  actual_levels <- names(x)
  count[actual_levels] <- x
  # Return the modified count vector as a table object
  as.table(count)
}))

# Transpose the result if needed to align variables as rows instead of columns
combined_table2 <- t(combined_table2)

# France

variable_names <- c("framing_parity_pro1"  ,         "framing_parity_pro2"     ,    "framing_quota_pro1"     ,       "framing_quota_pro2")

list_of_tables <- lapply(variable_names, function(var) table(df[[var]]))

# Define all possible levels across the 16 variables
levels_vector <- c("Necessary", "Style", "Policy", "Democracy", "Half of Population",
                   "Political Benefits", "Pace", "Other Countries", "Other Women")

# Bind all tables together by row names, ensuring all levels are considered
combined_table <- do.call(cbind, lapply(list_of_tables, function(x) {
  # Initialize a vector with all counts set to 0 for each level
  count <- setNames(rep(0, length(levels_vector)), levels_vector)
  # Update the counts with actual values from the table
  actual_levels <- names(x)
  count[actual_levels] <- x
  # Return the modified count vector as a table object
  as.table(count)
}))

# Transpose the result if needed to align variables as rows instead of columns
combined_table <- t(combined_table)


variable_names <- c("framing_parity_anti1",  "framing_parity_anti2"  ,  "framing_quota_anti1" ,  "framing_quota_anti2")

list_of_tables <- lapply(variable_names, function(var) table(df[[var]]))

# Define all possible levels across the 16 variables
levels_vector <- c("Unnecessary", "Lower Impact", "Suspicion", "Unqualified Candidates",
                   "Volvo Owners Next", "Uncertain Benefits", "History Shows", 
                   "Not Issue of Law", "Only Treat Symptoms")

# Bind all tables together by row names, ensuring all levels are considered
combined_table2 <- do.call(cbind, lapply(list_of_tables, function(x) {
  # Initialize a vector with all counts set to 0 for each level
  count <- setNames(rep(0, length(levels_vector)), levels_vector)
  # Update the counts with actual values from the table
  actual_levels <- names(x)
  count[actual_levels] <- x
  # Return the modified count vector as a table object
  as.table(count)
}))

# Transpose the result if needed to align variables as rows instead of columns
combined_table2 <- t(combined_table2)



## correlation between gender and masculinity and femininity

dim(all)



# Ensure the gender variable is numeric
all$resp_gender2 <- as.numeric(all$resp_gender == "Male")  # Converts "Male" to 1, others to 0

# Calculate point biserial correlation
correlation <- cor(all$resp_gender2, all$Masculine, method = "pearson")
print(correlation)
#0.6351444


correlation <- cor(all$resp_gender2, all$Feminine, method = "pearson")
print(correlation)
#-0.6725936



## DV = masculinity, is there an effect?


all2 <- subset(all, country == "UK")
all3 <- subset(all, country == "FR")

all3$resp_gender <- droplevels(all3$resp_gender)

res1 <- lm(Masculine ~ Treatment*resp_gender, data = all, weights = Weight)
summary(res1)

res2 <- lm(Masculine ~ Treatment*resp_gender, data = all2, weights = Weight)
summary(res2)

res3 <- lm(Masculine ~ Treatment*resp_gender, data = all3, weights = Weight)
summary(res3)


res3 <- lm(Masculine ~ Treatment, data = subset(all3, resp_gender=="Male"), weights = Weight)
summary(res3)



#### control function approach, checking our masculinity finding


fr <- subset(all, country == "FR")
fr <- fr[fr$Treatment != "Control", ]
fr$Treatment <- droplevels(fr$Treatment)
fr$resp_gender <- droplevels(fr$resp_gender)

fr_men <- fr[fr$resp_gender == "Male", ]
dim(fr_men)

# confirming effect 

res <- lm(Masculine ~ Treatment, data = fr_men, weights = Weight)
summary(res) # there is an effect at p<0.05

res <- lm(Masculine ~ Treatment, data = fr_men, weights = Weight)
summary(res)

res_full <- lm(Masculine ~ Treatment, data = fr_men, weights = Weight)
summary(res_full)

stargazer(res_full, title="Regression Results", align=TRUE, type="latex")


stargazer(res, title="Regression Results", align=TRUE, type="latex")

# implementing first stage of CFA

combined_masculine_model <- lm(Masculine ~ Treatment*resp_gender + resp_age, data = fr, weights = Weight)
summary(combined_masculine_model)

res4 <- lm(Support ~ resp_age, data = fr, weights = Weight)
summary(res4) #good does not impact dv=support at conventional levels of significance

stargazer(combined_masculine_model, title="Regression Results", align=TRUE, type="latex")

# extract residuals

fr$combined_masculine_resid <- residuals(combined_masculine_model)

# include in main model


res4 <- lm(Support ~ Treatment*Masculine*resp_gender + combined_masculine_resid, data = fr, weights = Weight)
summary(res4)

stargazer(res4, title="Regression Results", align=TRUE, type="latex")


# SDS sample investigation

# use appendix.r code to create this sample

dim(uksd)

dim(frsd)

# final dataset should be 1629

# Concatenate the two datasets
final_dataset <- rbind(uksd, frsd)

nrow(final_dataset) #1699
# Check the number of rows in each dataset
nrow(uksd)
nrow(frsd)

final <- final_dataset


final$resp_gender <- droplevels(final$resp_gender)


res1 <- lm(Masculine ~ Treatment*resp_gender, data = final, weights = Weight)
summary(res1)


uksd$resp_gender <- droplevels(uksd$resp_gender)
frsd$resp_gender <- droplevels(frsd$resp_gender)

#UK

res1 <- lm(Masculine ~ Treatment*resp_gender, data = uksd, weights = Weight)
summary(res1)

#FR

res1 <- lm(Masculine ~ Treatment*resp_gender, data = frsd, weights = Weight)
summary(res1)


##### replicating backlash model with dv=vote choice

## UK

fem <- subset(long, feature1=="Female")

fem$resp_gender <- relevel(fem$resp_gender, ref = "Female")



res19 <- lm(choice2 ~ Treatment, data = fem, weights = Weight)
summary(res19)


res21 <- lm(choice2 ~ Treatment*resp_gender, data = fem, weights = Weight)
summary(res21)


res23 <- lm(choice2 ~ Treatment*resp_gender*Masculine, data = fem, weights = Weight)
summary(res23)


## FR

fem <- subset(long, feature1=="Female")

fem$resp_gender <- relevel(fem$resp_gender, ref = "Female")



res25 <- lm(choice2 ~ Treatment, data = fem, weights = Weight)
summary(res25)


res27 <- lm(choice2 ~ Treatment*resp_gender, data = fem, weights = Weight)
summary(res27)


res29 <- lm(choice2 ~ Treatment*resp_gender*Masculine, data = fem, weights = Weight)
summary(res29)

stargazer(res19, res21, res23, res25, res27, res29, title="Regression Results", align=TRUE, type="latex")


## summary stats and independence

#UK

weighted_mean <- weighted.mean(df$resp_age, df$Weight)
#48

df$female_indicator <- ifelse(df$resp_gender == "Female", 1, 0)
weighted_proportion_female <- weighted.mean(df$female_indicator, df$Weight)
# 52%

df$resp_gender <- relevel(df$resp_gender, ref = "Female")

df$educ_ind <- ifelse((df$resp_education == "Degree" | df$resp_education == "Other technical things"), 1, 0)


df$resp_education <- factor(df$resp_education, levels = c("None", "Other technical things", "GCSE or equivalent", "A Level or equivalent", "Higher than A Level (without degree)", "Degree", "Don't Know", "Other"))

# Check the new order of levels
levels(df$resp_education)


weighted_proportion_educ <- weighted.mean(df$educ_ind, df$Weight)
#0.43

df$race_ind <- ifelse((df$resp_race == "British" | df$resp_race == "Irish" | df$resp_race == "Gypsy or Irish Traveller" | df$resp_race == "Any other White background"), 1, 0)

weighted_proportion_race <- weighted.mean(df$race_ind, df$Weight)
#89

library(dplyr)

# Recoding 'df$resp_race' into fewer categories
df <- df %>% 
  mutate(resp_race = recode(resp_race,
                            "British" = "White",
                            "Irish" = "White",
                            "Gypsy or Irish Traveller" = "White",
                            "Any other White background" = "White",
                            "White and Black Caribbean" = "Mixed",
                            "White and Black African" = "Mixed",
                            "White and Asian" = "Mixed",
                            "Multiple ethnic background" = "Mixed",
                            "Indian" = "Asian",
                            "Pakistani" = "Asian",
                            "Bangladeshi" = "Asian",
                            "Chinese" = "Asian",
                            "Any other Asian background" = "Asian",
                            "African" = "Black",
                            "Caribbean" = "Black",
                            "Any other Black or African background" = "Black",
                            "Arab" = "Other",
                            "Any other ethnic group" = "Other",
                            "Prefer not to say" = "Prefer not to say"))

# Converting 'resp_race' into a factor with 'White' as the baseline
df$resp_race <- factor(df$resp_race, levels = c("White", "Black", "Asian", "Mixed", "Other", "Prefer not to say"))

# Check the new levels
levels(df$resp_race)


df$party_ind <- ifelse((df$resp_party == "Conservative" | df$resp_party == "Brexit Party"), 1, 0)
weighted_proportion_party <- weighted.mean(df$party_ind, df$Weight, na.rm = TRUE)
#45%



# Chi-Square Test for Gender
gender_table <- table(df$resp_gender, df$Treatment)
chisq_gender <- chisq.test(gender_table)
chisq_gender

# Chi-Square Test for Education
education_table <- table(df$resp_education, df$Treatment)
chisq_education <- chisq.test(education_table)
chisq_education

# Chi-Square Test for Race
race_table <- table(df$resp_race, df$Treatment)
chisq_race <- chisq.test(race_table)
chisq_race

# Chi-Square Test for Political Party
party_table <- table(df$resp_party, df$Treatment)
chisq_party <- chisq.test(party_table)
chisq_party



# FR

weighted_mean <- weighted.mean(df$resp_age, df$Weight)
#50

df$female_indicator <- ifelse(df$resp_gender == "Female", 1, 0)
weighted_proportion_female <- weighted.mean(df$female_indicator, df$Weight)
#52%

df$resp_gender <- relevel(df$resp_gender, ref = "Female")

df$educ_ind <- ifelse((df$resp_education == "Baccalauréat" | df$resp_education == "University Degree" | df$resp_education == "Secondary University Degree"), 1, 0)
weighted_proportion_educ <- weighted.mean(df$educ_ind, df$Weight)
#68%

df$party_ind <- ifelse(df$resp_voteR2 == "Marine Le Pen (Rassemblement National)", 1, 0)
weighted_proportion_party <- weighted.mean(df$party_ind, df$Weight, na.rm = TRUE)
#37%



# Chi-Square Test for Gender
gender_table <- table(df$resp_gender, df$Treatment)
chisq_gender <- chisq.test(gender_table)
chisq_gender

# Chi-Square Test for Education
education_table <- table(df$resp_education, df$Treatment)
chisq_education <- chisq.test(education_table)
chisq_education


# Chi-Square Test for Political Party
party_table <- table(df$resp_voteR2, df$Treatment)
chisq_party <- chisq.test(party_table)
chisq_party


### Age

## UK

# Calculate the quintiles for the age variable
quintile_breaks <- quantile(df$resp_age, probs = seq(0, 1, by = 0.2), na.rm = TRUE)

# Cut the age data into quintiles
df$age_quintiles <- cut(df$resp_age, breaks = quintile_breaks, include.lowest = TRUE, labels = FALSE)

# Convert the quintile data into a factor
df$age_quintiles <- factor(df$age_quintiles, labels = c("1st Quintile", "2nd Quintile", "3rd Quintile", "4th Quintile", "5th Quintile"))

# View the first few rows of the modified data frame to verify the changes
head(df)


# Chi-Square Test for Age
age_table <- table(df$age_quintiles, df$Treatment)
chisq_age <- chisq.test(age_table)
chisq_age


## FR

# Calculate the quintiles for the age variable
quintile_breaks <- quantile(df$resp_age, probs = seq(0, 1, by = 0.2), na.rm = TRUE)

# Cut the age data into quintiles
df$age_quintiles <- cut(df$resp_age, breaks = quintile_breaks, include.lowest = TRUE, labels = FALSE)

# Convert the quintile data into a factor
df$age_quintiles <- factor(df$age_quintiles, labels = c("1st Quintile", "2nd Quintile", "3rd Quintile", "4th Quintile", "5th Quintile"))

# View the first few rows of the modified data frame to verify the changes
head(df)


# Chi-Square Test for Age
age_table <- table(df$age_quintiles, df$Treatment)
chisq_age <- chisq.test(age_table)
chisq_age


# using merged all data

# creating main table 1 ::: dv = support for uk and france first


# UK

uk <- subset(all, country=="UK")

uk2 <- uk[uk$Treatment != "Control", ]
uk2$Treatment <- droplevels(uk2$Treatment)

uk2$resp_gender <- droplevels(uk2$resp_gender )

res1 <- lm(Support ~ Treatment, data = uk2, weights = Weight)
summary(res1)

res2 <- lm(Support ~ Treatment*resp_gender, data = uk2, weights = Weight)
summary(res2)

res3 <- lm(Support ~ Treatment*resp_gender*Masculine, data = uk2, weights = Weight)
summary(res3)

resg1 <- lm(Support ~ Treatment + resp_gender, data = uk2, weights = Weight)
summary(resg1)



### weighted means

uk2$Support2<-NA
uk2$Support2[uk2$Support==1]<-0
uk2$Support2[uk2$Support==2]<-0
uk2$Support2[uk2$Support==3]<-0
uk2$Support2[uk2$Support==4]<-1
uk2$Support2[uk2$Support==5]<-1
table(uk2$Support2)

uk2$Oppose<-NA
uk2$Oppose[uk2$Support==1]<-1
uk2$Oppose[uk2$Support==2]<-1
uk2$Oppose[uk2$Support==3]<-0
uk2$Oppose[uk2$Support==4]<-0
uk2$Oppose[uk2$Support==5]<-0
table(uk2$Oppose, uk2$Treatment)


# France

fr <- subset(all, country=="FR")

fr2 <- fr[fr$Treatment != "Control", ]
fr2$Treatment <- droplevels(fr2$Treatment)

fr2$resp_gender <- droplevels(fr2$resp_gender )

res4 <- lm(Support ~ Treatment, data = fr2, weights = Weight)
summary(res4)

res5 <- lm(Support ~ Treatment*resp_gender, data = fr2, weights = Weight)
summary(res5)

res6 <- lm(Support ~ Treatment*resp_gender*Masculine, data = fr2, weights = Weight)
summary(res6)

resg2 <- lm(Support ~ Treatment + resp_gender, data = fr2, weights = Weight)
summary(resg2)


### weighted means

fr2$Support2<-NA
fr2$Support2[fr2$Support==1]<-0
fr2$Support2[fr2$Support==2]<-0
fr2$Support2[fr2$Support==3]<-0
fr2$Support2[fr2$Support==4]<-1
fr2$Support2[fr2$Support==5]<-1
table(fr2$Support2)

fr2$Oppose<-NA
fr2$Oppose[fr2$Support==1]<-1
fr2$Oppose[fr2$Support==2]<-1
fr2$Oppose[fr2$Support==3]<-0
fr2$Oppose[fr2$Support==4]<-0
fr2$Oppose[fr2$Support==5]<-0
table(fr2$Oppose, fr2$Treatment)


# creating main table 3 ::: dv = qualifications for uk then france 

# use the specific to country conjoint datasets for this

# UK

long$resp_gender <- factor(long$resp_gender, levels = c("Female", "Male"))


res13 <- lm(Qualified_score ~ Treatment, data = subset(long, feature1=="Female"), weights = Weight)
summary(res13)


res15 <- lm(Qualified_score ~ Treatment*resp_gender, data = subset(long, feature1=="Female"), weights = Weight)
summary(res15)


res17 <- lm(Qualified_score ~ Treatment*resp_gender*Masculine, data = subset(long, feature1=="Female"), weights = Weight)
summary(res17)


# FR

long$resp_gender <- factor(long$resp_gender, levels = c("Female", "Male"))

res19 <- lm(Qualified_score ~ Treatment, data = subset(long, feature1=="Female"), weights = Weight)
summary(res19)

res21 <- lm(Qualified_score ~ Treatment*resp_gender, data = subset(long, feature1=="Female"), weights = Weight)
summary(res21)

res23 <- lm(Qualified_score ~ Treatment*resp_gender*Masculine, data = subset(long, feature1=="Female"), weights = Weight)
summary(res23)



### social desirability subset

## UK first

vars<-c("ID", "Put_On_Show", "Center_Of_Attention", "Acting_Skills")
MyData2<-uk[,vars]

head(MyData2)

library(haven)
library(psych)

bias_keys<-make.keys(MyData2, list(MyData2=c(2,3,4)))
bias_var<-scoreItems(bias_keys, MyData2)
bias_scores<-bias_var$scores
MyData2$bias_var<-bias_scores[,]

mean(MyData2$bias_var)
#mean is 1.45

### Now merge the composite variable back to df data.frame 

vars<-c("ID", "bias_var")
X2<-MyData2[,vars]
head(X2)

test2<- merge(uk, X2, by=c("ID"), all.x=TRUE) 
head(test2)
nrow(test2) 

another <- test2

## Anyway, subset the data to those who score less than the mean of the bias variable (less likely
## to give socially desirable answers) 

another$unbiased<-0
another$unbiased[another$bias_var<1.45098]<-1
table(another$unbiased)

unbiased <- another[ which(another$unbiased==1), ]
nrow(unbiased) 
## 1108

uksd <- unbiased

## france next

vars<-c("ID", "Put_On_Show", "Center_Of_Attention", "Acting_Skills")
MyData2<-fr[,vars]

head(MyData2)
nrow(MyData2)

bias_keys<-make.keys(MyData2, list(MyData2=c(2,3,4)))
bias_var<-scoreItems(bias_keys, MyData2)
bias_scores<-bias_var$scores
MyData2$bias_var<-bias_scores[,]

mean(MyData2$bias_var)
#mean is 1.55973

### Now merge the composite variable back to df data.frame 

vars<-c("ID", "bias_var")
X2<-MyData2[,vars]
head(X2)

test2<- merge(fr, X2, by=c("ID"), all.x=TRUE) 
head(test2)
nrow(test2) 

another <- test2

## Anyway, subset the data to those who score less than the mean of the bias variable (less likely
## to give socially desirable answers) 

another$unbiased<-0
another$unbiased[another$bias_var<1.55973]<-1
table(another$unbiased)

unbiased <- another[ which(another$unbiased==1), ]
nrow(unbiased) 
## 591

frsd <- unbiased

## re-run analysis 

## for ease, apply below and then run from top 

uk <- uksd
fr <- frsd
nrow(uk)
nrow(fr)

# for conjoint, open respective datasets, then run the below, then re-run conjoint analysis above. 

# UK

dim(long)
#3468

# Keeping only rows in main_df with IDs also present in ref_df
merged_long <- merge(long, uk[, "ID", drop = FALSE], by = "ID", all.x = FALSE)
nrow(merged_long)

long <- merged_long 
nrow(long)

# run UK conjoint analysis above

# now for France

dim(long)
#2076

# Keeping only rows in main_df with IDs also present in ref_df
merged_long <- merge(long, fr[, "ID", drop = FALSE], by = "ID", all.x = FALSE)
nrow(merged_long)

long <- merged_long 
nrow(long)

# run FR conjoint analysis above


######### FMC subset

# load all merged dataset (fresh)


table(all$fmac_parity)
table(all$fmac_quota)

subset_df <- all %>%
  filter(fmac_parity == "Correct" | fmac_quota == "Correct")

uk <- subset(subset_df, country=="UK")
nrow(uk) #401

fr <- subset(subset_df, country=="FR")
nrow(fr) #180

#uk$Treatment <- droplevels(uk$Treatment)
#fr$Treatment <- droplevels(fr$Treatment)

# Run code from top again for support



# conjoint subset below

# UK

dim(long)
#3468

# Keeping only rows in main_df with IDs also present in ref_df
merged_long <- merge(long, uk[, "ID", drop = FALSE], by = "ID", all.x = FALSE)
nrow(merged_long) #1944

long <- merged_long 
nrow(long)

# run UK conjoint analysis above

# now for France

dim(long)
#2076

# Keeping only rows in main_df with IDs also present in ref_df
merged_long <- merge(long, fr[, "ID", drop = FALSE], by = "ID", all.x = FALSE)
nrow(merged_long)

long <- merged_long 
nrow(long)

# run france conjoint analysis next


###### Country differences #######

### country differences in support 
### use all merged data

## subset to UK and FR, treated only


sub2<-subset(all, Treatment != "Control")
nrow(sub2)

res_a <- lm(Support ~ country*Treatment, data = sub2, weights = Weight)
summary(res_a)

#### sds sample

## run code above to create uksd and frsd, then convert both to uk and fr, then follow code below

# Use rbind to merge the datasets
merged_dataset <- rbind(uk, fr)

# Check the number of rows in the merged dataset
nrow(merged_dataset) # this should be 591 + 1019 = 1610

sub2<-subset(merged_dataset, Treatment != "Control")
nrow(sub2)
#1103

res_b <- lm(Support ~ country*Treatment, data = sub2, weights = Weight)
summary(res_b)
## results are stronger now with sds sample!!!


#### fmc sample
merged_dataset <- rbind(uk, fr)
nrow(merged_dataset) #401 + 180 = 581

sub2  <- merged_dataset

res_c <- lm(Support ~ country*Treatment, data = sub2, weights = Weight)
summary(res_c)
## even stronger!

stargazer(res_a, res_b, res_c, title="Regression Results", align=TRUE, type="latex")

###################
### List Experiment##
###################

# are people more upset at thought of positive action measure?

load("all_merged.RData")

dim(all)

subset_fr <- all[all$country == "FR", ]
subset_uk <- all[all$country == "UK", ]


# UK

subset_uk$list_treated2 <- ifelse(subset_uk$list_treated == 0, "Control", "Treated")
subset_uk$list_treated2 <- factor(subset_uk$list_treated2)

res1 <- lm(lcount ~ list_treated2, weight=Weight, data=subset_uk)
summary(res1)

# FR

subset_fr$list_treated2 <- ifelse(subset_fr$list_treated == 0, "Control", "Treated")
subset_fr$list_treated2 <- factor(subset_fr$list_treated2)

res2 <- lm(lcount ~ list_treated2, weight=Weight, data=subset_fr)
summary(res2)



# I am going to pool our treatments and then I will check to see if any differences by framing

# UK

subset_uk$Treatment2 <- ifelse(subset_uk$Treatment=="Control", "Control", "Parity or Quota Treatment")

res7 <- lm(lcount ~ list_treated2*Treatment2, weight=Weight, data=subset_uk)
summary(res7)
# no difference between control v pooling main Treatment in UK

# FR

subset_fr$Treatment2 <- ifelse(subset_fr$Treatment=="Control", "Control", "Parity or Quota Treatment")

res8 <- lm(lcount ~ list_treated2*Treatment2, weight=Weight, data=subset_fr)
summary(res8)
# no difference between control v pooling main Treatment in FR

# perhaps diff between countries is significant
merged_dataset <- rbind(subset_fr, subset_uk)
nrow(merged_dataset)

res8_b <- lm(lcount ~ country*list_treated2*Treatment2, data = merged_dataset, weights = Weight)
summary(res8_b)
# nope

# let's check for framing differences next and see

#UK
res9 <- lm(lcount ~ list_treated2*Treatment, weight=Weight, data=subset_uk)
summary(res9)

#FR
res10 <- lm(lcount ~ list_treated2*Treatment, weight=Weight, data=subset_fr)
summary(res10)
# no differences by framing for either country

res10_b <- lm(lcount ~ country*list_treated2*Treatment, data = merged_dataset, weights = Weight)
summary(res10_b)
# nope











